/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.library.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.base.ApiResult;
import com.library.entity.BookUserEntity;
import com.library.entity.vo.BookUserVo;
import com.library.entity.vo.UserVo;
import com.library.service.BookUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源使用方
 *
 * @author pig
 * @date 2023-06-29 09:59:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("ruralLibrary/user")
@Api(tags = "管理员", value = "管理员")
public class BookUserController {

	@Resource
	private BookUserService bookUserService;

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @return
	 */
	@ApiOperation(value = "分页查询")
	@GetMapping("/page")
	public ApiResult getbookUserPage(Page page, UserVo userVo) {
		return bookUserService.getUser(page, userVo);
	}

	/**
	 * 通过id查询资源使用方
	 *
	 * @param id id
	 * @return ApiResult
	 */
	@ApiOperation(value = "通过id查询")
	@GetMapping("/{id}")
	public ApiResult getById(@PathVariable("id") Long id) {
		return bookUserService.getOne(id);
	}

	/**
	 * 新增资源使用方
	 *
	 * @param bookUser 资源使用方
	 * @return ApiResult
	 */
	@ApiOperation(value = "新增资源使用方")
	@PostMapping
	public ApiResult save(@RequestBody BookUserEntity bookUser) {
		ApiResult r = bookUserService.saveUser(bookUser);
		System.out.println(r);
		return r;
	}

	/**
	 * 修改资源使用方
	 *
	 * @param bookUser 资源使用方
	 * @return ApiResult
	 */
	@ApiOperation(value = "修改资源使用方")
	@PostMapping("/updateById")
	public ApiResult updateById(@RequestBody UserVo bookUser) {
		return bookUserService.update(bookUser);
	}

	/**
	 * 通过id删除资源使用方
	 *
	 * @param ids id列表
	 * @return ApiResult
	 */
	@ApiOperation(value = "通过id删除资源使用方")
	@PostMapping("/delete")
	public ApiResult removeById(@RequestBody Long[] ids) {
		return bookUserService.remove(ids);
	}

	/**
	 * 导出excel 表格
	 *
	 * @return excel 文件流
	 */
	@ApiOperation(value ="")
	@GetMapping("/export")
	public List<BookUserVo> export(UserVo userVo) {
		return bookUserService.export(userVo);
	}

	/**
	 * 导入用户
	 *
	 * @param excelVOList   用户列表
	 * @param bindingResult 错误信息列表
	 * @return ApiResult
	 */
	@PostMapping("/import")
	public ApiResult importUser(@RequestBody List<BookUserVo> excelVOList, BindingResult bindingResult) {
		return bookUserService.importUser(excelVOList, bindingResult);
	}

	@GetMapping("/{bucket}/{fileName}")
	public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) {
		bookUserService.getFile(bucket, fileName, response);
	}

	@PostMapping("/saveBookUser")
	public ApiResult saveBookUser(BookUserEntity bookUser) {
		return bookUserService.saveUser(bookUser);
	}
	@PostMapping("/login")
	public ApiResult login(String userName, String userCode) {
		return bookUserService.login(userName,userCode);
	}

}
